package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.LoginInfo;
import cn.itcast.service.LoginServcie;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author coolbook
 * @ClassName LoginController
 * @Description TODO
 * @Date 17:18 2021/11/15
 * @Version 1.0
 */
@RestController
@RequestMapping("member")
@Slf4j
@CrossOrigin
public class LoginController {
    @Reference
    private LoginServcie loginServcie;

    /**
     * 登录功能
     *
     * @param loginInfo
     * @param model
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody LoginInfo loginInfo, Model model) {

        try {
            log.info(loginInfo.getPhone() + "====" + loginInfo.getValidateCode());
            if (ObjectUtils.isEmpty(loginInfo)) {
                //传来的号码 和验证码都为空 直接返回验证消息  (手机和验证码都不能为空)
                return new Result(true, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
            }
            Boolean loginFlag = this.loginServcie.login(loginInfo.getPhone(), loginInfo.getValidateCode());
            if (loginFlag) {
                return new Result(true, MessageConstant.LOGIN_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.VALIDATECODE_ERROR);
    }

}
